<?php
/**
 * Accountresume
 *
 * PHP version 5
 *
 * @category Model
 * @package  Croogo
 * @version  1.0
 * @author   Tran Van Dung <dungtv@ic-t.info>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.croogo.org
 */
class Accountresume extends AppModel {
/**
 * Model name
 *
 * @var string
 * @access public
 */
    public $name = 'Accountresume';
/**
 * Behaviors used by the Model
 *
 * @var array
 * @access public
 */
    public $actsAs = array(
//        'ExtendAssociations',
        'Encoder',
        'Cached' => array(
            'prefix' => array(
                'aihcontact_',
            ),
        ),
    );
/**
 * Validation
 *
 * @var array
 * @access public
 */
    public $validate = array(
        'date' => array(
            'rule' => 'notEmpty',
            'message' => 'This field cannot be left blank.',
        ),
        'from_date' => array(
            'rule' => 'notEmpty',
            'message' => 'This field cannot be left blank.',
        ),
        'to_date' => array(
            'rule' => 'notEmpty',
            'message' => 'This field cannot be left blank.',
        ),
        'sum_insured' => array(
            'rule' => 'notEmpty',
            'message' => 'This field cannot be left blank.',
        ),
        'rate' => array(
            'rule' => 'notEmpty',
            'message' => 'This field cannot be left blank.',
        ),
        'order_hereon' => array(
            'rule' => 'notEmpty',
            'message' => 'This field cannot be left blank.',
        ),
    );
/**
 * Model associations: belongsTo
 *
 * @var array
 * @access public
 */
    public $belongsTo = array(
        'Insurancetype' => array(
            'className' => 'Insurancetype',
            'foreignKey' => 'insurancetype_id',
            'conditions' => '',
            'fields' => '',
            'order' => '',
        ),
    );
    
    public function delete_payment($ids=array(), $resume_refid='') {
        if($resume_refid!='') {
            return $this->query("DELETE FROM `cc_paymentschedules` WHERE `accountresume_id` = '".  $resume_refid ."'");
        }
        if($ids!=null) {
            return $this->query("DELETE FROM `cc_paymentschedules` WHERE `id` IN (".  implode(',', $ids).") ");
//            return $this->query("UPDATE `cc_paymentschedules` AS `Paymentschedule` SET `Paymentschedule`.`deleted`=1 WHERE `Paymentschedule`.`id` IN (".  implode(',', $ids).") ");
        }
        return false;
    }
    public function delete_placement($ids=array(), $resume_refid='') {
        if($resume_refid!='') {
            return $this->query("DELETE FROM `cc_placementdetailts` WHERE `accountresume_id` = '".  $resume_refid ."'");
        }
        if($ids!=null) {
            return $this->query("DELETE FROM `cc_placementdetailts` WHERE `id` IN (".  implode(',', $ids).") ");
//            return $this->query("UPDATE `cc_placementdetailts` AS `Placementdetailt` SET `Placementdetailt`.`deleted`=1 WHERE `Placementdetailt`.`id` IN (".  implode(',', $ids).") ");
        }
        return false;
    }
    public function delete_reminder($paymentschedule_ids=array()) {
        if($paymentschedule_ids!=null) {
            return $this->query("DELETE FROM `cc_reminders` WHERE `paymentschedule_id` IN (".  implode(',', $paymentschedule_ids).") ");
        }
        return false;
    }
    public function reminder($data=array()) {
        if($data) {
            if(isset($data['update'])) {
                unset($data['update']);
                // update
                $sql = 'UPDATE `cc_reminders` SET ';
                $set = '';
                foreach ($data as $k => $v) {
                    $set .= $set=='' ? "`".$k."`='".$v."'" : ", `".$k."`='".$v."'";
                }
                $sql .= $set . " WHERE `paymentschedule_id`=".$data['paymentschedule_id'];
                return $this->query($sql);
            } else {
                // insert
                $sql = 'INSERT INTO `cc_reminders` ';
                $fields = '';
                $values = '';
                foreach ($data as $k => $v) {
                    $fields .= $fields=='' ? "`".$k."`" : ", `".$k."`";
                    $values .= $values=='' ? "'".$v."'" : ", '".$v."'";
                }
                $sql .= "(" . $fields . ") VALUES (" . $values . ")";
                return $this->query($sql);
            }
        }
        return false;
    }
    public function lock($ids=array(), $tbl='cc_aihcontacts', $lock=1) { // $tbl='cc_markets' || $tbl='cc_insurancetypes'
        if($ids != null) {
            return $this->query("UPDATE `".$tbl."` AS `TBL` SET `TBL`.`lock`=`TBL`.`lock`+".$lock." WHERE `TBL`.`id` IN (".  implode(',', $ids).") ");
        } 
        return false;
    }
}
?>